﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RegCustomer;
using Infrastructure;
using System.Xml.Linq;
using System.Reflection;

namespace RunCommands
{
    class Program
    {
        static void Main(string[] args)
        {

            var bus = new FakeBus();
            var eventStore = new EventStore(bus);
            var repository = new Repository<Customer>(eventStore);
            var commandHandler = new CommandHandlers(repository);

            bus.RegisterHandler<CreateNewCustomerCommand>(commandHandler.Handle);
            bus.RegisterHandler<DeactivateCustomerCommand>(commandHandler.Handle);
            bus.RegisterHandler<UpdateCustomerCommand>(commandHandler.Handle);
            bus.RegisterHandler<MakeCustomerPriorityCommand>(commandHandler.Handle);
            bus.RegisterHandler<UpdateCustomerDateOfBirth>(commandHandler.Handle);

            StateMachine.SetupStateMachineFromXml(
                @"C:\Users\Sean\Desktop\Thesis\Sample Projects\reg-customer\RegCustomer\StateTransferFunction.xml"
            );

            Guid id = Guid.NewGuid();

            CreateNewCustomerCommand create = new CreateNewCustomerCommand(id, "sean", 0);
            bus.Send(create);

            UpdateCustomerCommand update = new UpdateCustomerCommand(id, "john", 1);
            bus.Send(update);

            UpdateCustomerDateOfBirth updateDob = new UpdateCustomerDateOfBirth(id, new DateTime(1910, 12, 4), 2);
            bus.Send(updateDob);

            DeactivateCustomerCommand markInvalid = new DeactivateCustomerCommand(id, "dodgy", 3);
            bus.Send(markInvalid);

            MakeCustomerPriorityCommand priority = new MakeCustomerPriorityCommand(id, 4);
            bus.Send(priority);

        }

        private static void SetupStateMachine()
        {

            State start = new State("Start");
            State active = new State("Active");
            State deactivated = new State("Deactivated");
            State priority = new State("Priority");

            start.AddTransition(
                typeof(CustomerCreatedEvent), active);

            active.AddTransition(
                typeof(CustomerDeactivatedEvent),
                deactivated);
            active.AddTransition(
                typeof(CustomerUpdatedEvent),
                active);
            active.AddTransition(
                typeof(CustomerMadePriorityEvent),
                priority);

            StateMachine.AddState(start);
            StateMachine.AddState(active);
            StateMachine.AddState(deactivated);
            StateMachine.AddState(priority);
            StateMachine.InitialState = start;

        }

        
    }
}
